package com.jinzhi.jzapi.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.jzweb.domain.ExaminationBatchComDO;
import com.jinzhi.jzweb.domain.MarkDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AuntTask {

    @Autowired
    private StuSignUpComService stuSignUpService;
    @Autowired
    private ExaminationBatchComService examinationBatchComService;
    @Autowired
    private MarkService markService;
    @Autowired
    private ExaminationAnswerSheetComService examinationAnswerSheetService;
    // 使用Cron表达式执行定时任务
    @Scheduled(cron = "0 0 0 * * ?") // 每天晚上12点触发
//    @Scheduled(cron = "0 0/1 * * * ? ")//一分钟执行一次
//    @Scheduled(fixedRate = 60000)
    public void cronTask() {
        // 定时任务的逻辑处理
//        stuSignUpService.setDefaultComment();
        System.out.println("开始执行定时器");
        List<ExaminationBatchComDO> batchType = examinationBatchComService.selectList(new EntityWrapper<ExaminationBatchComDO>().eq("batch_type", "1"));
        batchType.forEach(e->{
            Long id = e.getId();
            System.out.println("批次id为====="+id);
            List<StuSignUpComDO> stuSignUpComDOS = stuSignUpService.selectList(new EntityWrapper<StuSignUpComDO>().eq("cbatch_id", id));
            System.out.println("该批次下是否有考生====="+stuSignUpComDOS.size());
            stuSignUpComDOS.forEach(f->{
                StuSignUpComDO s = stuSignUpService.selectById(f.getId());
                s.setState(8);//将考试转态“已考完”更改为“考试中”
                stuSignUpService.updateById(s);
                Map map = new HashMap<String,String>();
                map.put("stu_sign_id", f.getId());
                //删除考生成绩
                markService.delete(new EntityWrapper<MarkDO>().eq("sign_id",f.getId()));
                System.out.println("考生成绩已删除"+f.getId());
                boolean b=	examinationAnswerSheetService.deleteByMap(map);
            });
        });
        System.out.println("定时器执行完毕");
    }

}
